<?php

namespace app\controller;

use app\BaseController;
use app\service\WarehouseService;
use think\exception\ValidateException;
use \think\facade\Request;

/**
 * 仓库
 */
class Warehouse extends BaseController
{
    /**
     * @desc 仓库类型 业务类型
     * @return void
     */
    public function info()
    {
        $info = WarehouseService::info();
        returnSuccess(200, '获取成功', $info);
    }

    //仓库列表
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['uid'] = $uid;
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['is_export'] = Request::param('is_export/d', 0);
        $logParam['ip'] = Request::ip();
        $data = WarehouseService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //新增仓库
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        // 1 快递 2社区团购 3 经销商
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['person_in_charge'] = Request::param('person_in_charge/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['goods_type'] = Request::param('goods_type/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['enterprise_name'] = Request::param('enterprise_name/s', '');
        $param['address'] = Request::param('address/s', '');
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['warehouse_type_id'] = Request::param('warehouse_type_id/d', 0);
        $param['warehouse_reserves'] = Request::param('warehouse_reserves/s', '');
        $param['warehouse_square_id'] = Request::param('warehouse_square_id/d', 0);
        $param['business_id'] = Request::param('business_id/s', '');
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\Warehouse::class)->check([
                'name' => $param['name'],
                'business_type_id' => $param['business_type_id'],
                'person_in_charge' => $param['person_in_charge'],
                'mobile' => $param['mobile'],
                'goods_type' => $param['goods_type'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'enterprise_name' => $param['enterprise_name'],
                'address' => $param['address'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'warehouse_type_id' => $param['warehouse_type_id'],
                'warehouse_reserves' => $param['warehouse_reserves'],
                'warehouse_square_id' => $param['warehouse_square_id'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = WarehouseService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改仓库
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['person_in_charge'] = Request::param('person_in_charge/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['goods_type'] = Request::param('goods_type/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['enterprise_name'] = Request::param('enterprise_name/s', '');
        $param['address'] = Request::param('address/s', '');
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['warehouse_type_id'] = Request::param('warehouse_type_id/d', 0);
        $param['warehouse_reserves'] = Request::param('warehouse_reserves/s', '');
        $param['warehouse_square_id'] = Request::param('warehouse_square_id/d', 0);
        $param['business_id'] = Request::param('business_id/s', '');
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\Warehouse::class)->check([
                'name' => $param['name'],
                'business_type_id' => $param['business_type_id'],
                'person_in_charge' => $param['person_in_charge'],
                'mobile' => $param['mobile'],
                'goods_type' => $param['goods_type'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'enterprise_name' => $param['enterprise_name'],
                'address' => $param['address'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'warehouse_type_id' => $param['warehouse_type_id'],
                'warehouse_reserves' => $param['warehouse_reserves'],
                'warehouse_square_id' => $param['warehouse_square_id'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = WarehouseService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除仓库
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = WarehouseService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '添加成功', $data);
        } else {
            returnFail(21101, '添加失败');
        }
    }
}
